package com.weisw.polaris.admin.controller;

import com.weisw.polaris.admin.model.Article;
import com.weisw.polaris.admin.model.Menu;
import com.weisw.polaris.admin.service.impl.ArticleServiceImpl;
import com.weisw.polaris.admin.service.impl.MenuServiceImpl;
import com.weisw.polaris.core.http.HttpResult;
import com.weisw.polaris.core.page.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户控制器
 *
 * @author Louis
 * @date Jan 13, 2019
 */
@RestController
@RequestMapping("article")
public class ArticleController {

    @Autowired
    private ArticleServiceImpl articleService;

    @PostMapping(value = "/save")
    public HttpResult save(@RequestBody Article record) {
        if (record.getTitle() == null){
            return HttpResult.error("请输入文章名");
        }
        Article article = articleService.findById(record.getArticleId());
        if (article == null) {
            // 新增文章
//            if (articleService.findByName(record.getTitle()) != null) {
                return HttpResult.error("文章已存在!");
//            }

        }
        return HttpResult.ok(articleService.save(record));
    }

    @PostMapping(value = "/delete")
    public HttpResult delete(@RequestBody List<Article> records) {
        return HttpResult.ok(articleService.delete(records));
    }

//    @GetMapping(value = "/findByName")
//    public HttpResult findByUserName(@RequestParam String name) {
//        return HttpResult.ok(articleService.findByName(name));
//    }

    @PostMapping(value = "/findPage")
    public HttpResult findPage(@RequestBody PageRequest pageRequest) {
        return HttpResult.ok(articleService.findPage(pageRequest));
    }

}
